package demo1;

class Money implements Cloneable{
    public int m = 95;
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
class Person implements Cloneable{
    Money money = new Money();
    public String name;
    public int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person tmp = (Person) super.clone();
        tmp.money = (Money) this.money.clone();
        return tmp;
    }
}

public class test3 {

    public static void main(String[] args) throws CloneNotSupportedException {

        Person person1 = new Person("张三", 25);
        Person person2 = (Person) person1.clone();
        System.out.println("修改前的结果：");
        System.out.println(person1.money.m);
        System.out.println(person2.money.m);

        person1.money.m = 15;
        System.out.println("修改后的结果：");
        System.out.println(person1.money.m);
        System.out.println(person2.money.m);
    }
}
